﻿// -----------------------------------------------------------------------
// <copyright file="EventLogData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.COMMON.NonPersistents;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db EventLog
    /// </summary>
    public class EventLogData
    {
        /// <summary>
        /// Get EventLog By Id
        /// </summary>
        /// <param name="eventLogId">Param eventLogId</param>
        /// <returns>Return EventLog</returns>
        public EventLogEN GetEventLogById(int eventLogId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                EventLogEN eventLog = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEventLogGetById, eventLogId);
                while (reader.Read())
                {
                    eventLog = new EventLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        eventLog.EventLogId = reader.GetDecimal(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        eventLog.UserName = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        eventLog.EventName = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        eventLog.CreatedTime = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        eventLog.Status = reader.GetBoolean(4);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return eventLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all EventLog
        /// </summary>
        /// <returns>Return list all EventLog</returns>
        public IList<EventLogEN> GetEventLogs()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<EventLogEN> lstEventLog = new List<EventLogEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEventLogGetAll);
                while (reader.Read())
                {
                    var eventLog = new EventLogEN();

                    eventLog = new EventLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        eventLog.EventLogId = reader.GetDecimal(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        eventLog.UserName = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        eventLog.EventName = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        eventLog.CreatedTime = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        eventLog.Status = reader.GetBoolean(4);
                    }

                    lstEventLog.Add(eventLog);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstEventLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db EventLog
        /// </summary>
        /// <param name="eventLog">Param EventLog</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(EventLogEN eventLog)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.UserName, eventLog.UserName),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.EventName, eventLog.EventName),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.CreatedTime, eventLog.CreatedTime),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.Status, eventLog.Status)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEventLogInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db EventLog
        /// </summary>
        /// <param name="eventLog">Param EventLog</param>
        public void Update(EventLogEN eventLog)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.EventLogId, eventLog.EventLogId),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.UserName, eventLog.UserName),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.EventName, eventLog.EventName),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.CreatedTime, eventLog.CreatedTime),
                                    new SqlParameter(Constants.SqlParamerterName.EventLog.Status, eventLog.Status)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEventLogUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete EventLog by eventLogId
        /// </summary>
        /// <param name="eventLogId">param eventLogId</param>
        public void Delete(decimal eventLogId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.EventLog.EventLogId, eventLogId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEventLogDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete All
        /// </summary>
        public void DeleteAll()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEventLogDeleteAll);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets all EventLogEN paging.
        /// </summary>
        /// <param name="searchLogNP">param searchLogNP</param>
        /// <returns>return list EventLogEN</returns>
        public IList<EventLogEN> GetAllEventLogPaging(SearchLogNP searchLogNP)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<EventLogEN> lstEventLog = new List<EventLogEN>();

                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.UserName, searchLogNP.UserName),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.FromDate, searchLogNP.FromDate),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.ToDate, searchLogNP.ToDate),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.PageIndex, searchLogNP.PageIndex),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.PageSize, searchLogNP.PageSize),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.EventName, !string.IsNullOrWhiteSpace(searchLogNP.EventName) ? searchLogNP.EventName.Trim() : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.SiteLog.TotalRecords, searchLogNP.TotalRecords)
                                };

                sqlParameters[6].Direction = ParameterDirection.Output;

                SqlDataReader reader = SqlHelper.ExecuteReader(
                    SqlHelper.ConnectionString, CommandType.StoredProcedure, Constants.StorepProcedure.VOneEventLogGetAllPaging, sqlParameters);

                while (reader.Read())
                {
                    var eventLog = new EventLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        eventLog.EventLogId = reader.GetDecimal(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        eventLog.UserName = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        eventLog.EventName = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        eventLog.CreatedTime = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        eventLog.Status = reader.GetBoolean(4);
                    }

                    lstEventLog.Add(eventLog);
                }

                reader.Close();

                searchLogNP.TotalRecords = Convert.ToInt32(sqlParameters[6].Value);

                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstEventLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}